﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using CaseReview.Model;
using CaseReview.Utility;

namespace Utility
{
    public class SendMail
    {
        public static void SendEmail(string to, string body,string subject) {
            SmtpClient client = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            client.Host = "GTECPDD.fareast.corp.microsoft.com";
            client.Port = 25;
            client.UseDefaultCredentials = true;
            if(to!=null) {
                string[] address = to.Split(';');
                foreach (string s in address) {
                    mailMessage.To.Add(s);
                }
            }
            MailAddress mailAddress = new MailAddress("procase@microsoft.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject;
            mailMessage.BodyEncoding = Encoding.GetEncoding(936);

            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            client.Send(mailMessage);
        }

        public static void SendEmail(string to,string body,string subject,string cc) {
            SmtpClient client = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            client.Host = "GTECPDD.fareast.corp.microsoft.com";
            client.Port = 25;
            client.UseDefaultCredentials = true;
            if(to != null) {
                string[] address = to.Split(';');
                foreach (string s in address) {
                    mailMessage.To.Add(s);
                }
            }
            if(cc != null && cc!="") {
                string[] address = cc.Split(';');
                foreach (string s in address) {
                    mailMessage.CC.Add(s);
                }
            }
            MailAddress mailAddress = new MailAddress("procase@microsoft.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject;
            mailMessage.BodyEncoding = Encoding.GetEncoding(936);

            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            client.Send(mailMessage);
        }
        public static void SendReviewerMail(IList<CaseInfo>caseInfos ) {
            string body;
            try {
                StreamReader reader = new StreamReader(Config.EmailTemp);
                body = reader.ReadToEnd();
            }
            catch (Exception) {
                return;
            }
            StringBuilder temp = new StringBuilder();
            foreach (CaseInfo caseInfo in caseInfos) {
                temp.Append(@"<tr><td>");
                temp.Append(caseInfo.CaseId).Append(@"</td><td>").Append(caseInfo.Title).Append(@"</td><td>")
                    .Append(caseInfo.Age).Append(@"</td><td>").Append(caseInfo.Labor).Append(@"</td><td>")
                    .Append(caseInfo.Idle).Append(@"</td><td>").Append(caseInfo.Owner).Append(@"</td><td>").Append(caseInfo.ReviewerName).Append(@"</td><tr>");
            }
            body = body.Replace("@@overall", temp.ToString());
            SendEmail(Config.ReviewerAddress, body, Config.Subject,Config.Cc);
        }

        public static void SendReminderMail(IList<CaseInfo> caseInfos) {
            string body;
            try {
                StreamReader reader = new StreamReader(Config.EmailTemp);
                body = reader.ReadToEnd();
            }
            catch (Exception) {
                return;
            }
            StringBuilder temp = new StringBuilder();
            foreach (CaseInfo caseInfo in caseInfos) {
                temp.Append(@"<tr><td>");
                temp.Append(caseInfo.CaseId).Append(@"</td><td>").Append(caseInfo.Title).Append(@"</td><td>")
                    .Append(caseInfo.Age).Append(@"</td><td>").Append(caseInfo.Labor).Append(@"</td><td>")
                    .Append(caseInfo.Idle).Append(@"</td><td>").Append(caseInfo.Owner).Append(@"</td><td>").Append(caseInfo.ReviewerName).Append(@"</td><tr>");
            }
            body = body.Replace("@@overall", temp.ToString());
            SendEmail(Config.ReviewerAddress, body, Config.ReminderSubject, Config.Cc);
        }
    }
}
